#define DBC_CHECK_PRECONDITION

using System;
using System.Xml;

using DesignByContract;

using Outcoding.GraphGenerator.Entities;

namespace Outcoding.GraphGenerator.XmlBuilders {
    public class GraphXmlBuilder : IXmlBuilder {

        private Graph mResultObject = null;

        public object ResultObject {
            get {
                return mResultObject;
            }
        }

        public GraphXmlBuilder(XmlNode graphXmlNode) {
            Check.Require(graphXmlNode != null, "Xml source node cannot be null");
            Check.Require(graphXmlNode.Name.CompareTo(XmlTagNames.graph.ToString()) == 0, "A Graph Xml node must be supplied.");
            this.BuildObject(graphXmlNode);
        }

        private void BuildObject(XmlNode graphXmlNode) {
            mResultObject = new Graph();
            try {
                mResultObject.Type = ParseTypeAttribute(graphXmlNode);
            } catch (DesignByContract.PreconditionException preExc) {
                //log error
                mResultObject = null;
                throw;
            }
        }

        private ChartType ParseTypeAttribute(XmlNode graphXmlNode) {
            Check.Require(graphXmlNode.Attributes["type"] != null, "index attribute not present");
            return ConvertTypeFromName(graphXmlNode.Attributes["type"].Value);
        }

        private ChartType ConvertTypeFromName(string typeName) {
            Check.Require(typeName != null, "Type name not defined");

            ChartType type = ChartType.xlUnknown;
            switch (typeName) {
                case "xlLine":
                    type = ChartType.xlLine;
                    break;
                case "xlLineMarkers":
                    type = ChartType.xlLineMarkers;
                    break;
                case "xlColumnClustered":
                    type = ChartType.xlColumnClustered;
                    break;
                case "xlColumnStacked":
                    type = ChartType.xlColumnStacked;
                    break;
                case "xlColumnStacked100":
                    type = ChartType.xlColumnStacked100;
                    break;
                case "xl3DColumn":
                    type = ChartType.xl3DColumn;
                    break;
                case "xl3DColumnClustered":
                    type = ChartType.xl3DColumnClustered;
                    break;
                case "xl3DColumnStacked":
                    type = ChartType.xl3DColumnStacked;
                    break;
                case "xl3DColumnStacked100":
                    type = ChartType.xl3DColumnStacked100;
                    break;
                case "xlBarClustered":
                    type = ChartType.xlBarClustered;
                    break;
                case "xlBarStacked":
                    type = ChartType.xlBarStacked;
                    break;
                case "xlBarStacked100":
                    type = ChartType.xlBarStacked100;
                    break;
                case "xl3DBarClustered":
                    type = ChartType.xl3DBarClustered;
                    break;
                case "xl3DBarStacked":
                    type = ChartType.xl3DBarStacked;
                    break;
                case "xl3DBarStacked100":
                    type = ChartType.xl3DBarStacked100;
                    break;
                default:
                    //unknown
                    break;
            }
            return type;
        }
    }
}
